package com.yb0os1.controller.admin;

import com.yb0os1.dto.SetmealDTO;
import com.yb0os1.entity.Setmeal;
import com.yb0os1.result.PageResult;
import com.yb0os1.result.Result;
import com.yb0os1.serve.SetmealServe;
import com.yb0os1.vo.DishItemVO;
import com.yb0os1.vo.SetmealVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
@Tag(name = "套餐相关接口")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealServe setmealServe;

    @Operation(summary = "新增套餐")
    @PostMapping
    @CacheEvict(cacheNames = "setmeal",key = "#setmealDTO.categoryId")
    public Result<String> addSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealServe.addSetmeal(setmealDTO);
        return Result.success();
    }

    @Operation(summary = "根据套餐id查询套餐")
    @GetMapping("{id}")
    public Result<SetmealVO> getSetmealById(@PathVariable Long id) {
        return Result.success(setmealServe.getSetmealById(id));
    }

    @Operation(summary = "分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageSelect(SetmealDTO setmealDTO,Integer page ,Integer pageSize){
        PageResult pageResult = setmealServe.pageSelect(setmealDTO.getCategoryId(), setmealDTO.getName(), setmealDTO.getStatus(), page, pageSize);

        log.info("分页查询结果{}",pageResult);
        return Result.success(pageResult);
    }

    @Operation(summary = "修改套餐售卖状态")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public Result<String> changeStatus(Long id,@PathVariable Integer status){
        setmealServe.changeStatus(id,status);
        return Result.success();
    }

    @Operation(summary = "批量删除")
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @DeleteMapping
    public Result<String> delete(@RequestParam List<Long> ids){
        setmealServe.deletes(ids);
        return Result.success();
    }

    @Operation(summary = "修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public Result<String> updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealServe.updateSetmeal(setmealDTO);
        return Result.success();
    }
}
